package com.ruoyi.odorservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.odorservice.entity.*;
import com.ruoyi.odorservice.entity.vo.OptionsVo;
import com.ruoyi.odorservice.model.vo.SiteInfoVo;
import com.ruoyi.odorservice.model.vo.SiteNameVo;
import com.ruoyi.odorservice.service.SampleService;
import com.ruoyi.odorservice.service.SiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/site")
@Api(tags = "受体点位")
@AllArgsConstructor
public class SiteController extends BaseController {
    private SiteService siteService;
    private SampleService sampleService;
    @GetMapping("/page")
    @ApiOperation(value = "分页列表",httpMethod = "GET", response = Site.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "siteName", value = "点位名称", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "siteLabel", value = "公司id", paramType = "query", dataType = "String", required = false)
    })
    public TableDataInfo getSiteInfoList(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                              @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                                              @RequestParam(value = "siteName", required = false) String siteName,
                                                    @RequestParam(value = "siteLabel", required = false) String siteLabel) {
        startPage();
        List<Site> list = siteService.list(new LambdaQueryWrapper<Site>()
                .like(StringUtils.isNotEmpty(siteName), Site::getSiteName, siteName)
                .like(StringUtils.isNotEmpty(siteLabel), Site::getSiteLabel, siteLabel)
        );
        return getDataTable(list);
    }

    @GetMapping("/siteOptions")
    @ApiOperation(value = "列表（Options）", httpMethod = "GET")
    public Result<List<OptionsVo>> getIndustries() {
        List<Site> industries = siteService.list(new LambdaQueryWrapper<Site>()
                .select(Site::getSiteName, Site::getSiteLabel, Site::getId));
        return Result.ok(industries.stream().map(item -> {
            OptionsVo optionsVo = new OptionsVo();
            optionsVo.setLabel(item.getSiteName()+"("+item.getSiteLabel()+")");
            optionsVo.setValue(item.getId()+"");
            return optionsVo;
        }).collect(Collectors.toList()));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增点位",httpMethod = "POST", response = Boolean.class)
    public Result<Boolean> createOdor(@RequestBody Site site){
        if(StringUtils.isNull(site)){
            Result.fail("点位信息不能为空");
        }
        if (StringUtils.isEmpty(site.getSiteName()) || StringUtils.isNull(site.getSiteName())) {
            return Result.fail("点位名称不能为空");
        }
        if (StringUtils.isNull(site.getLongitude()) || StringUtils.isNull(site.getLatitude())) {
            return Result.fail("点位经纬度不能为空");
        }
        Boolean createState = siteService.createSite(site);
        return Result.ok(createState);
    }

    @GetMapping("detail/{id}")
    @ApiOperation(value = "点位详情", httpMethod = "GET", response = StandardFiles.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", paramType = "query", dataType = "String", required = false)
    })
    public Result<Site> details(@PathVariable(value = "id") Integer id) {
        return Result.ok(siteService.getById(id));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改点位",httpMethod = "PUT", response = Boolean.class)
    public Result<Boolean> updateSite(@RequestBody Site site){
        if(StringUtils.isNull(site)){
            Result.fail("点位信息不能为空");
        }
        if(site.getId() == null){
            return Result.fail("点位id不能为空");
        }
        if (StringUtils.isEmpty(site.getSiteName()) || StringUtils.isNull(site.getSiteName())) {
            return Result.fail("点位名称不能为空");
        }
        if ( StringUtils.isNull(site.getLongitude()) || StringUtils.isNull(site.getLatitude())) {
            return Result.fail("点位经纬度不能为空");
        }
        Boolean updateState = siteService.updateSite(site);
        return Result.ok(updateState);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除点位",httpMethod = "DELETE", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "点位id", paramType = "query", dataType = "Integer", required = true)
    })
    public Result<Boolean> deleteSite(@RequestParam(value = "id") Integer id){
        if(StringUtils.isNull(id)){
            Result.fail("点位id不能为空");
        }
        Boolean deleteState = siteService.deleteSite(id);
        return Result.ok(deleteState);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取点位列表",httpMethod = "GET", response = SiteNameVo.class)
    public Result<List<Site>> getSiteList(){
        List<Site> list = siteService.list();
        return Result.ok(list);
    }

    @GetMapping("/site/sample/options")
    @ApiOperation(value = "获取点位样本列表（Options）",httpMethod = "GET", response = SiteNameVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "点位id", paramType = "query", dataType = "Integer", required = true)
    })
    public Result<List<OptionsVo>> sampleOptions(@RequestParam(value = "id") Integer id){
        Site site = siteService.getById(id);
        if(site == null){
            return Result.fail("点位不存在");
        }
        List<Sample> list = sampleService.list(new LambdaQueryWrapper<Sample>()
                .eq(Sample::getSiteName, site.getSiteName())
                .select(Sample::getId, Sample::getSiteName, Sample::getSampleTime)
        );
        return Result.ok(list.stream().map(item -> {
            OptionsVo optionsVo = new OptionsVo();
            optionsVo.setLabel(item.getSiteName()+"("+item.getSampleTime().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+")");
            optionsVo.setValue(item.getId()+"");
            return optionsVo;
        }).collect(Collectors.toList()));
    }
}
